<template>
  <div>
    <!-- 搜索栏区域 -->
    <el-card class="SearchCard"
             body-style="padding:0px">
      <el-row>
        <el-col :span="2">
          <div class="SearchTitle">条件搜索栏</div>
        </el-col>
        <el-col :span="4">
          <el-row>
            <el-col :span="10">
              <div class="SearchLable">搜索关键字</div>
            </el-col>

            <el-col :span="14">
              <div class="SearchLable">
                <el-input placeholder="请输入内容"
                          v-model="queryInfo.srm"
                          @clear="clearEvent"
                          clearable>
                </el-input>
              </div>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="2">
          <div class="SearchLable">
            <el-button size="medium"
                       type="primary"
                       @click="getSearchList()">搜索</el-button>
          </div>
        </el-col>
        <el-col :span="2">
          <div class="SearchLable">
            <el-button size="medium"
                       @click="dialogVisible=true"
                       type="primary">新增检查申请</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <el-card class="TableCard"
             :body-style="{ padding: '0px' }">
      <div>
        <el-table :data="TableData"
                  border
                  :header-cell-style="{ 'text-align':'center',background: '#3F9DFD', color: '#FFF' }"
                  :cell-style="{'text-align':'center'}"
                  size="medium"
                  stripe
                  style="width: 100%;font-size:12px">
          <el-table-column prop="examcode"
                           label="检查编码"
                           width="100">
          </el-table-column>
          <el-table-column prop="name"
                           label="操作"
                           width="60">
            <template slot-scope="scope">
              <el-button type="text"
                         size="mini"
                         @click="showExamOne(scope.row.id)">编辑</el-button>
            </template>

          </el-table-column>
          <el-table-column prop="name"
                           label="删除"
                           width="60">
            <template slot-scope="scope">
              <el-button type="text"
                         size="mini"
                         @click="deleteDictExam(scope.row.id)">删除</el-button>
            </template>

          </el-table-column>
          <el-table-column prop="examname"
                           label="检查名称">
          </el-table-column>
          <el-table-column prop="type"
                           label="检查类型"
                           width="200">
            <template slot-scope="scope">
              {{(scope.row.type==3)?"磁共振":(scope.row.type==1)?"X射线":(scope.row.type==2)?"CT":"默认值"}}
            </template>
          </el-table-column>
          <!-- 类型1X射线2CT3磁共振 -1是默认值 -->
        </el-table>
      </div>
      <div class="block">
        <el-pagination @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="queryInfo.pagenum"
                       :page-sizes="[20, 50, 100]"
                       :page-size="queryInfo.pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="this.total"
                       background>
        </el-pagination>
      </div>
    </el-card>
    <el-dialog title="新增检查申请"
               :visible.sync="dialogVisible"
               width="40%"
               :before-close="handleClose">
      <el-card>
        <el-form label-position="right"
                 label-width="80px"
                 :model="addNewDictExamData">
          <el-form-item label="检查编码"
                        label-width="20%">
            <el-input v-model="addNewDictExamData.examcode"
                      placeholder="请输入关键字"></el-input>
          </el-form-item>
          <el-form-item label="检查名称"
                        label-width="20%">
            <el-input v-model="addNewDictExamData.examname"
                      placeholder="请输入关键字"></el-input>
          </el-form-item>
          <el-form-item label="检查类型"
                        label-width="20%">
            <el-select v-model="addNewDictExamData.type"
                       placeholder="请选择">
              <el-option v-for="item in options"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

        </el-form>
      </el-card>

      <span slot="footer"
            class="dialog-footer">
        <el-button type="primary"
                   size="mini"
                   @click="addNewDictExam()">新增</el-button>
        <el-button size="mini"
                   @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>

    <!-- 编辑新项目 -->
    <el-dialog title="编辑检查申请"
               :visible.sync="addExamVisible"
               width="40%"
               :before-close="handleClose">
      <el-card>
        <el-form label-position="right"
                 label-width="80px"
                 :model="showExamData">
          <el-form-item label="检查编码"
                        label-width="20%">
            <el-input v-model="showExamData.examcode"
                      placeholder="请输入关键字"></el-input>
          </el-form-item>
          <el-form-item label="检查名称"
                        label-width="20%">
            <el-input v-model="showExamData.examname"
                      placeholder="请输入关键字"></el-input>
          </el-form-item>
          <el-form-item label="检查类型"
                        label-width="20%">
            <el-select v-model="showExamData.type"
                       placeholder="请选择">
              <el-option v-for="item in options"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

        </el-form>
      </el-card>

      <span slot="footer"
            class="dialog-footer">
        <el-button type="primary"
                   size="mini"
                   @click="updateDictExam()">更新</el-button>
        <el-button size="mini"
                   @click="addExamVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getExamAllList, addNewDictExam, showExamOne, updateDictExam, deleteDictExam } from "@/api/rad"
export default {
  data () {
    return {
      queryInfo: {
        srm: '',
        pageList: 1,
        pageSize: 20,
      },
      total: 0,
      TableData: [],
      dialogVisible: false,
      addNewDictExamData: {
        examcode: "",
        examname: "",
        type: ""
      },
      // 1X射线2CT3磁共振 默认值-1
      options: [{
        value: 1,
        label: 'X射线'
      },
      {
        value: 2,
        label: 'CT'
      },
      {
        value: 3,
        label: '磁共振'
      },
      {
        value: -1,
        label: '默认值'
      }],
      addExamVisible: false,
      showExamData: {
        id: "",
        examcode: "",
        examname: "",
        type: ""
      }
    }
  },
  created: function () {
    this.getExamAllList()
  },
  methods: {
    handleClose (done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getExamAllList()
    },

    handleCurrentChange (newPage) {
      this.queryInfo.pageList = newPage
      this.getExamAllList()
    },
    clearEvent () {
      this.getExamAllList()
    },
    getSearchList () {
      this.getExamAllList()
    },
    async getExamAllList () {
      let res = await this.$rad.getExamAllList({
        srm: this.queryInfo.srm,
        pageList: this.queryInfo.pageList,
        pageSize: this.queryInfo.pageSize
      })
      this.TableData = res.rows
      this.total = res.total
      console.log(res)
    },
    async addNewDictExam () {
      let res = await this.$rad.addNewDictExam({
        examcode: this.addNewDictExamData.examcode,
        examname: this.addNewDictExamData.examname,
        type: this.addNewDictExamData.type
      })
      console.log(res)
      if (res.code == 1) {
        this.$message.success(res.msg)
        this.addNewDictExamData.examcode = ""
        this.addNewDictExamData.examname = ""
        this.addNewDictExamData.type = " "
        this.dialogVisible = false
        this.getExamAllList()
      } else {
        this.$message.error(res.msg)
      }
    },
    async showExamOne (id) {
      this.showExamData.id = id
      let res = await this.$rad.showExamOne({
        id: id
      })
      console.log(res)
      this.showExamData.examcode = res.data.examcode
      this.showExamData.examname = res.data.examname
      this.showExamData.type = res.data.type
      this.addExamVisible = true
    },
    async updateDictExam () {
      let res = await this.$rad.updateDictExam({
        id: this.showExamData.id,
        examcode: this.showExamData.examcode,
        examname: this.showExamData.examname,
        type: this.showExamData.type
      })
      console.log(res)
      if (res.code == 1) {
        this.$message.success(res.msg)
        this.showExamData.examcode = ""
        this.showExamData.examname = ""
        this.showExamData.type = " "
        this.addExamVisible = false
        this.getExamAllList()
      } else {
        this.$message.error(res.msg)
      }
    },
    async deleteDictExam (id) {
      let res = await this.$rad.deleteDictExam({
        id: id
      })
      console.log(res)
      if (res.code == 1) {
        this.$message.success(res.msg)
        this.getExamAllList()
      } else {
        this.$message.error(res.msg)
      }
    }
  }
}
</script>
<style>
.SearchCard {
  height: 50px;
  font-size: 12px;
}
.SearchTitle {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-right: 1px solid #c2c2c2;
}
.SearchLable {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.TableCard {
  margin-left: 3px;
  margin-top: 2px;
}
.block {
  text-align: center;
  margin-top: 12px;
  margin-bottom: 12px;
}
</style>